home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / selectv1.tar / selectv1 / screenio.c < prev    next >
C/C++ Source or Header  |  1993-09-04  |  8KB  |  448 lines

  1. /*
  2. screenio.c: screen io procedures for selectnews
  3.  
  4. Copyright (C) 1993 Eugene Eric Kim
  5. All rights reserved.
  6.  
  7. LAST REVISION: September 4, 1993
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "headers.h"
  13.  
  14. void Credits()
  15. {
  16.   standout();
  17.   move(LINES-1,0);
  18.   clrtoeol();
  19.   addstr("selectnews * Copyright (C) Eugene Kim 1993 * Press 'i' for instructions");
  20.   standend();
  21.   refresh();
  22. }
  23.  
  24. void Topline()
  25. {
  26. #ifdef NCURSES 
  27.   attron(A_BOLD);
  28. #else
  29.   standout();
  30. #endif
  31.   mvaddstr(0,0,"UNSUBSCRIBED");
  32.   mvaddstr(0,COLS - (COLS / 2),"SUBSCRIBED");
  33. #ifdef NCURSES
  34.   attroff(A_BOLD);
  35. #else
  36.   standend();
  37. #endif
  38. }
  39.  
  40. void initwin()
  41. {
  42.   initscr();
  43.   noecho();
  44.   cbreak();
  45. #ifdef NCURSES
  46.   keypad(stdscr,TRUE);
  47. #endif
  48.   leftscr = newwin(LINES-2,COLS / 2,1,0);
  49.   rightscr = newwin(LINES-2,COLS - (COLS / 2),1,COLS - (COLS / 2));
  50.   scrollok(leftscr,TRUE);
  51.   scrollok(rightscr,TRUE);
  52.   Topline();
  53.   Credits();
  54. }
  55.  
  56. void Instructions()
  57. {
  58.   clear();
  59.   standout();
  60.   mvaddstr(3,0,"MOVEMENT");
  61.   mvaddstr(3,40,"SUBSCRIBE/UNSUBSCRIBE");
  62.   mvaddstr(8,40,"MISCELLANEOUS");
  63.   mvaddstr(12,0,"QUIT");
  64.   mvaddstr(16,0,"PRESS ANY KEY TO CONTINUE");
  65.   standend();
  66.   mvaddstr(0,0,"selectnews");
  67.   mvaddstr(0,11,VERSION);
  68.   mvaddstr(1,0,"Copyright (C) 1993 Eugene Eric Kim");
  69.   mvaddstr(4,0,"up             = k");
  70.   mvaddstr(5,0,"down           = j");
  71.   mvaddstr(6,0,"switch windows = <space>, h, l");
  72.   mvaddstr(7,0,"scroll down    = >");
  73.   mvaddstr(8,0,"scroll up      = <");
  74.   mvaddstr(9,0,"beginning      = 0");
  75.   mvaddstr(10,0,"end            = $");
  76.   mvaddstr(4,40,"(un)subscribe item = <return>");
  77.   mvaddstr(5,40,"(un)subscribe all  = g");
  78.   mvaddstr(9,40,"sort buffer = s");
  79.   mvaddstr(10,40,"find item   = f");
  80.   mvaddstr(13,0,"quit w/o saving        = q");
  81.   mvaddstr(14,0,"write .newsrc and quit = w");
  82.   refresh();
  83. }
  84.  
  85. short Sure(str)
  86. char str[80];
  87. {
  88.   char ch;
  89.  
  90.   move(LINES-1,0);
  91.   clrtoeol();
  92.   addstr(str);
  93.   refresh();
  94.   do
  95.     ch = getch();
  96.   while ( (ch!='y') && (ch!='Y') && (ch!='n') && (ch!='N') );
  97.   if ( (ch == 'y') || (ch == 'Y') )
  98.     return 1;
  99.   else {
  100.     Credits();
  101.     return 0;
  102.   }
  103. }
  104.  
  105. void cursor(win,loc,str)
  106. WINDOW *win;
  107. int loc;
  108. char* str;
  109. {
  110.   mvwaddstr(win,loc,0,str);
  111.   wrefresh(win);
  112. }
  113.  
  114. void delcursor(win,loc)
  115. WINDOW *win;
  116. int loc;
  117. {
  118.   mvwaddstr(win,loc,0,"  ");
  119.   wrefresh(win);
  120. }
  121.  
  122. void showentry(win,b,line)
  123. WINDOW *win;
  124. int b;
  125. char line[LEN];
  126. {
  127.   char temp[LEN];
  128.   int i = 0;
  129.  
  130.   while ( (line[i]!=':') && (line[i]!='!') ) {
  131.     temp[i] = line[i];
  132.     i++;
  133.   }
  134.   temp[i] = '\0';
  135.   if (i > COLS/2 - 2) {
  136.     i = COLS/2-2;
  137.     temp[i-4] = ' ';
  138.     temp[i-3] = '-';
  139.     temp[i-2] = '>';
  140.     temp[i-1] = '\0';
  141.   }
  142.   mvwaddstr(win,b,2,temp);
  143. }
  144.  
  145. void showbuffer(win,buffer,start)
  146. WINDOW *win;
  147. dllist buffer;
  148. int start;
  149. {
  150.   node* w;
  151.   int i = 0;
  152.  
  153.   w = list_position(buffer,start);
  154.   werase(win);
  155.   while ((w!=0) && (i<LINES-2)) {
  156.     showentry(win,i,(*w).entry);
  157.     w = list_next(w);
  158.     i++;
  159.   }
  160.   wrefresh(win);
  161. }
  162.  
  163. node* moveup(win,cpos,top,w,cchar)
  164. WINDOW *win;
  165. int *cpos, *top;
  166. node* w;
  167. char* cchar;
  168. {
  169.   if ((*cpos) || (*top))
  170.     w = list_prev(w);
  171.   if ((*cpos)) {
  172.     delcursor(win,*cpos);
  173.     (*cpos)--;
  174.     cursor(win,*cpos,cchar);
  175.   }
  176.   else if ((*top)) { /* (*cpos) = 0 */
  177.     (*top)--;
  178.     delcursor(win,*cpos);
  179.     wmove(win,0,0);
  180.     winsertln(win);
  181.     showentry(win,0,(*w).entry);
  182.     cursor(win,(*cpos),cchar);
  183.     wrefresh(win);
  184.   }
  185.   return w;
  186. }
  187.  
  188. node* movedown(win,cpos,top,list,w,cchar)
  189. WINDOW *win;
  190. int *cpos,*top;
  191. dllist list;
  192. node* w;
  193. char* cchar;
  194. {
  195.   if (list_next(w) != 0) {
  196.     w = list_next(w);
  197.     if ((*cpos)<LINES-3) {
  198.       delcursor(win,*cpos);
  199.       (*cpos)++;
  200.       cursor(win,*cpos,cchar);
  201.     }
  202.     else { /* bottom of screen */
  203.       (*top)++;
  204.       delcursor(win,*cpos);
  205.       wmove(win,0,0);
  206.       wdeleteln(win);
  207.       showentry(win,*cpos,(*w).entry);
  208.       cursor(win,*cpos,cchar);
  209.       wrefresh(win);
  210.     }
  211.   }
  212.   return w;
  213. }
  214.  
  215. node* scrollup(win,cpos,top,list,w,cchar)
  216. WINDOW *win;
  217. int *cpos, *top;
  218. dllist list;
  219. node* w;
  220. char* cchar;
  221. {
  222.   int i = LINES - 2;
  223.  
  224.   if (*top) {
  225.     if ( (*top)-i > 0 )
  226.       (*top) -= i;
  227.     else {
  228.       (*top) = (*cpos) = 0;
  229.     }
  230.     showbuffer(win,list,*top);
  231.   }
  232.   delcursor(win,*cpos);
  233.   w = list_position(list,*top + *cpos);
  234.   cursor(win,*cpos,cchar);
  235.   return w;
  236. }
  237.  
  238. node* scrolldown(win,cpos,top,list,w,cchar)
  239. WINDOW *win;
  240. int *cpos, *top;
  241. dllist list;
  242. node* w;
  243. char* cchar;
  244. {
  245.   int i = LINES - 2;
  246.  
  247.   if (list_position(list,*top + *cpos + i) != 0) {
  248.     w = list_position(list,*top + *cpos + i);
  249.     (*top) += (*cpos) + i;
  250.     (*cpos) = 0;
  251.   }
  252.   else
  253.     (*cpos) = list_size(list) - (*top) - 1;
  254.   showbuffer(win,list,*top);
  255.   cursor(win,*cpos,cchar);
  256.   return w;
  257. }
  258.  
  259. node* homepage(win,cpos,top,list,cchar)
  260. WINDOW *win;
  261. int *cpos, *top;
  262. dllist list;
  263. char* cchar;
  264. {
  265.   (*top) = (*cpos) = 0;
  266.   showbuffer(win,list,*top);
  267.   cursor(win,*cpos,cchar);
  268.   return list_start(list);
  269. }
  270.  
  271. node* endpage(win,cpos,top,list,cchar)
  272. WINDOW *win;
  273. int *cpos, *top;
  274. dllist list;
  275. char* cchar;
  276. {
  277.   int i = list_size(list);
  278.   int screensize = LINES - 2;
  279.  
  280.   if (i > screensize) {
  281.     (*top) = i - screensize;
  282.     (*cpos) = screensize - 1;
  283.     showbuffer(win,list,(*top));
  284.     cursor(win,(*cpos),cchar);
  285.   }
  286.   else {
  287.     delcursor(win,(*cpos));
  288.     (*cpos) = i - 1;
  289.     cursor(win,(*cpos),cchar);
  290.   }
  291.   return list_end(list);
  292. }
  293.  
  294. short indexstr(index,len,src,dest,ch)
  295. int index,len;
  296. char src[80], (*dest)[80];
  297. char ch;
  298. {
  299.   int i = 0, found = 0, j, size;
  300.  
  301.   (*dest)[0] = '\0';
  302.   size = strlen(src);
  303.   while ( (i<size) && (found!=index) ) {
  304.     if (src[i]==ch) found++;
  305.     if (found!=index) i++;
  306.   }
  307.   if (found!=index)
  308.     return 0;
  309.   else {
  310.     if (size - i >= len) {
  311.       for (j = 0; j<len; j++)
  312.     (*dest)[j] = src[i+j];
  313.       (*dest)[j] = '\0';
  314.       return 1;
  315.     }
  316.     else
  317.       return 0;
  318.   }
  319. }
  320.  
  321. node* find(win,cpos,top,list,cchar)
  322. WINDOW *win;
  323. int *cpos,*top;
  324. dllist list;
  325. char* cchar;
  326. {
  327.   node* w;
  328.   node* v;
  329.   short found = 0, exist;
  330.   char what[80], search[80], temp[80];
  331.   int i = 0, j;
  332.  
  333.   move(LINES-1,0);
  334.   standend();
  335.   clrtoeol();
  336.   addstr("Find: ");
  337.   getstr(what);
  338.   if (what[0]!='\0') {
  339.     v = w = list_start(list);
  340.     while ( (w!=0) && (!found) ) {
  341.       j = 1;
  342.       while (exist = indexstr(j,strlen(what),(*w).entry,&search,what[0])) {
  343.     if (!strcmp(search,what)) {
  344.       found = 1;
  345.       (*top) = i;
  346.       (*cpos) = 0;
  347.       v = w;
  348.       showbuffer(win,list,(*top));
  349.       cursor(win,(*cpos),cchar);
  350.       if (!list_last(w) && Sure("Search again (y/n)? ")) {
  351.         found = j = 0;
  352.         w = list_next(w);
  353.         i++;
  354.       }
  355.     }
  356.         j++;
  357.       }
  358.       if (!exist)
  359.     w = list_next(w);
  360.       i++;
  361.     }
  362.   }
  363.   Credits();
  364.   showbuffer(win,list,(*top));
  365.   cursor(win,(*cpos),cchar);
  366.   return v;
  367. }
  368.  
  369. int cmp(entry1,entry2)
  370. char entry1[LEN], entry2[LEN];
  371. {
  372.   int i = 0, j = 0;
  373.  
  374.   while (alpha[i] != entry1[0]) i++;
  375.   while (alpha[j] != entry2[0]) j++;
  376.   if (i<j)
  377.     return 1;
  378.   else if (i>j)
  379.     return 0;
  380.   else {
  381.     i = 1;
  382.     if (strlen(entry1) < strlen(entry2))
  383.       j = strlen(entry1);
  384.     else
  385.       j = strlen(entry2);
  386.     while ( (entry1[i] == entry2[i]) && (i<=j) ) i++;
  387.     if (entry1[i]<entry2[i])
  388.       return 1;
  389.     else
  390.       return 0;
  391.   }
  392. }
  393.  
  394. void partition(list,low,high,pivotloc)
  395. dllist* list;
  396. int low,high;
  397. int *pivotloc;
  398. {
  399.   char pivot[LEN];
  400.   int i,lastsmall;
  401.   node* temp;
  402.  
  403.   list_interchange(list,list_position(*list,low),list_position(*list,(low+high)/2));
  404.   strcpy(pivot,(*list_position(*list,low)).entry);
  405.   lastsmall = low;
  406.   temp = list_position(*list,low+1);
  407.   for (i = low+1; i <= high; i++) {
  408.     if (cmp(temp,pivot)) {
  409.       lastsmall++;
  410.       list_interchange(list,temp,list_position(*list,lastsmall));
  411.     }
  412.     temp = list_next(temp);
  413.   }
  414.   list_interchange(list,list_position(*list,low),list_position(*list,lastsmall));
  415.   (*pivotloc) = lastsmall;
  416. }
  417.  
  418. void quicksort(list,low,high)
  419. dllist* list;
  420. int low,high;
  421. {
  422.   int pivotloc;
  423.  
  424.   if (low < high) {
  425.     partition(list,low,high,&pivotloc);
  426.     quicksort(list,low,pivotloc - 1);
  427.     quicksort(list,pivotloc + 1,high);
  428.   }
  429. }
  430.  
  431. void sort(win,list,top,cpos,cchar)
  432. WINDOW *win;
  433. dllist* list;
  434. int top, cpos;
  435. char* cchar;
  436. {
  437.   char ch;
  438.  
  439.   move(LINES-1,0);
  440.   clrtoeol();
  441.   addstr("Sorting... this may take a few minutes.");
  442.   refresh();
  443.   quicksort(list,0,list_size(*list) - 1);
  444.   showbuffer(win,*list,top);
  445.   cursor(win,cpos,cchar);
  446.   Credits();
  447. }
  448.